package com.sxf.common.param;

import com.sxf.common.enums.SystemCommonCode;
import com.sxf.consts.ISystemCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;

/**
 * 应答结果类，返回信息包括调用状态、可读的msg、结果数据.
 *
 * @author hanzhiwei
 * @date 2016年10月20日
 * @version 1.0
 */
@ApiModel("返回数据")
public class ResultInfo<T> implements Serializable {
	/**
	 * 状态常量
	 */
	public final static String STATUS = "status";
	/**
	 * 状态信息常量
	 */
	public final static String STATUS_INFO = "statusInfo";
	/**
	 * 数据常量
	 */
	public final static String DATA = "data";

	@ApiModelProperty("返回数据状态码：<a href='/swagger/code'>返回值编码对应表</a>")
	private int status = SystemCommonCode.SUCCESS;
	/** 操作结果描述信息 */
	@ApiModelProperty("返回数据状态码描述")
	private String statusInfo = "处理成功";
	/** 操作返回数据绑定 */
	@ApiModelProperty("返回数据结果集")
	private T data;
	/** result实体 */
	public static final ResultInfo<?> SUCCESSRESULT = new ResultInfo<Object>();

	public static <T> ResultInfo<T> error() {
		ResultInfo<T> res = new ResultInfo<T>(SystemCommonCode.BUSINESS_ERR, "处理失败");
		return res;
	}

	public static <T> ResultInfo<T> errorMessage(String errorMessage) {
		ResultInfo<T> res = new ResultInfo<T>(SystemCommonCode.BUSINESS_ERR, errorMessage);
		return res;
	}

	public static <T> ResultInfo<T> errorMessage(String errorMessage, T data) {
		ResultInfo<T> res = new ResultInfo<T>(SystemCommonCode.BUSINESS_ERR, errorMessage);
		res.setData(data);
		return res;
	}

	public static <T> ResultInfo<T> errorMessage(int code, String errorMessage) {
		ResultInfo<T> res = new ResultInfo<T>(code, errorMessage);
		return res;
	}

	public static <T> ResultInfo<T> result(int status, String info) {
		ResultInfo<T> res = new ResultInfo<T>();
		res.status = status;
		res.statusInfo = info;
		return res;
	}

	public static <T> ResultInfo<T> result(ISystemCode systemCode) {
		ResultInfo<T> res = new ResultInfo<T>();
		res.status = systemCode.getCode();
		res.statusInfo = systemCode.getMessage();
		return res;
	}

	public static <T> ResultInfo<T> result(int status, String info, T data) {
		ResultInfo<T> res = new ResultInfo<T>();
		res.status = status;
		res.statusInfo = info;
		res.data = data;
		return res;
	}

	public static <T> ResultInfo<T> success() {
		ResultInfo<T> res = new ResultInfo<T>();
		return res;
	}

	public static <T> ResultInfo<T> success(T data) {
		ResultInfo<T> res = new ResultInfo<T>();
		res.setData(data);
		return res;
	}

	public static <T> ResultInfo<T> successMessage(String message) {
		ResultInfo<T> res = new ResultInfo<T>(SystemCommonCode.SUCCESS, message);
		return res;
	}

	public ResultInfo() {
	}

	public ResultInfo(int status, String statusInfo) {
		this.status = status;
		this.statusInfo = statusInfo;
	}

	public ResultInfo(int status, String statusInfo, T data) {
		super();
		this.status = status;
		this.statusInfo = statusInfo;
		this.data = data;
	}

	public T getData() {
		return data;
	}

	public int getStatus() {
		return status;
	}

	public String getStatusInfo() {
		return statusInfo;
	}

	public void setData(T data) {
		this.data = data;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public void setStatusInfo(String statusInfo) {
		this.statusInfo = statusInfo;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((data == null) ? 0 : data.hashCode());
		result = prime * result + status;
		result = prime * result + ((statusInfo == null) ? 0 : statusInfo.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		ResultInfo<?> other = (ResultInfo<?>) obj;
		if (data == null) {
			if (other.data != null) {
				return false;
			}
		} else if (!data.equals(other.data)) {
			return false;
		}
		if (status != other.status) {
			return false;
		}
		if (statusInfo == null) {
			if (other.statusInfo != null) {
				return false;
			}
		} else if (!statusInfo.equals(other.statusInfo)) {
			return false;
		}
		return true;
	}

	/**
	 * tostring方法
	 * 
	 * @return
	 */
	@Override
	public String toString() {
		return "ResultInfo [status=" + status + ", statusInfo=" + statusInfo + ", data=" + data + "]";
	}
}
